<?php

class ScreenDisplay {

    public static $LMSG = "";
    private static $suspendPrints = false;

    public static function suspendMessages() {
        ScreenDisplay::$suspendPrints = true;
    }

    public static function resumeMessages() {
        ScreenDisplay::$suspendPrints = false;
    }

    public static function getScreenData($room, $name, $data) {
        $db = DBUtils::Create();

        $id = $room->getId();

        $lines_arr = $db->getScreenMessages($id, $name);
        $playerTurnNum = $room->getCurrentPlayerTurnNumber($name);
        $phase = $room->getPhase($name);

        $i = 0;
        $res = array();

        $lines_data = array();
        foreach ($lines_arr as $line_arr) {

            if ($phase != Phase_GameOver) {
                $turnnum = $line_arr['turnnum'];
                if ($playerTurnNum - $turnnum >= TURN_TRESHOLD)
                    continue;
            }

            $l = array();
            $full_line = $line_arr['line'];
            $line_tokens = explode("|", $full_line);

            $l['line'] = $line_tokens[0];
            $l['name'] = $line_arr['name'];


            $params = array();
            $k = 0;
            for ($j = 1; $j < sizeof($line_tokens); $j++) {
                $param_tokens = explode("~", $line_tokens[$j]);
                #this is not a param token that we want, so we append it to the regular line
                if (sizeof($param_tokens) < 2) {
                    $l['line'] .= $line_tokens[$j];
                    continue;
                }
                $params[$k]['name'] = $param_tokens[0];
                $params[$k]['d_name'] = $param_tokens[1];
                $k++;
            }

            $l['params'] = $params;


            $lines_data[$i] = $l;
            $i += 1;
        }
        $res['lines'] = $lines_data;
        $data['screenmessages'] = $res;
        return $data;
    }

    public static function addScreenMessage($room, $isNewLine, $playerMessage, $otherMessages = null, $name = null, $shouldLog = true, $insertTab = true, $skipHeader = false) {
        if (ScreenDisplay::$suspendPrints)
            return;

        $gender = $room->getPlayerGender($name);
        if ($gender == 'female') {
            if ($playerMessage)
                $playerMessage = str_replace(" his", " her", $playerMessage);
            if ($otherMessages)
                $otherMessages = str_replace(" his", " her", $otherMessages);
            if ($otherMessages)
                $otherMessages = str_replace(" he ", "  she ", $otherMessages);
        }
        else if ($gender == 'it') {
            if ($playerMessage)
                $playerMessage = str_replace(" his", " its", $playerMessage);
            if ($otherMessages)
                $otherMessages = str_replace(" his", " its", $otherMessages);
            if ($otherMessages)
                $otherMessages = str_replace(" he ", "  it ", $otherMessages);
        }

        if (true == $shouldLog) {
            ScreenDisplay::addScreenMessageAndLog($room, $isNewLine, $playerMessage, $otherMessages, null, $name, $insertTab, $skipHeader);
        } else {
            ScreenDisplay::addScreenMessageOnly($room, $isNewLine, $playerMessage, $otherMessages, $name, $insertTab, $skipHeader);
        }
    }

    private static function addScreenMessageOnly($room, $isNewLine, $playerMessage, $otherMessages = null, $name = null, $insertTab = true, $skipHeader = false) {
        $db = DBUtils::Create();
        $id = $room->getId();
        $name = $room->getCurrentPlayerName($name);
        $turnNum = $room->getCurrentPlayerTurnNumber($name);

        $other_names = $room->getOtherPlayersNames(false, $name);
        $tab = "";
        if ($insertTab)
            $tab = "=====";
        if ($id == 0) {
            if ($playerMessage != null) {
                $db->setScreenMessage($isNewLine, $id, $name, "", $playerMessage, $turnNum);
            }

            if ($otherMessages != null) {
                foreach ($other_names as $o_name) {
                    $db->setScreenMessage($isNewLine, $id, $o_name, "", $otherMessages, $turnNum);
                }
            }
        } else {

            if ($playerMessage != null) {
                if ($playerMessage != NEW_LINE)
                    if ($skipHeader)
                        $db->setScreenMessage($isNewLine, $id, $name, $tab, $playerMessage, $turnNum);
                    else
                        $db->setScreenMessage($isNewLine, $id, $name, $tab . "You ", $playerMessage, $turnNum);
                else
                    $db->setScreenMessage($isNewLine, $id, $name, "", "", $turnNum);
            }

            if ($otherMessages != null) {
                foreach ($other_names as $o_name) {
                    if ($otherMessages != NEW_LINE)
                        if ($skipHeader)
                            $db->setScreenMessage($isNewLine, $id, $o_name, $tab, $otherMessages, $turnNum);
                        else
                            $db->setScreenMessage($isNewLine, $id, $o_name, $tab . "" . $name . " ", $otherMessages, $turnNum);
                    else
                        $db->setScreenMessage($isNewLine, $id, $o_name, "", "", $turnNum);
                }
            }
        }
    }

    private static function addLogMessage($room, $isNewLine, $message, $name = null, $insertTab = true, $skipHeader = false) {
        $db = DBUtils::Create();

        $id = $room->getId();
        $name = $room->getCurrentPlayerName($name);
        $tab = "";
        
        if ($message == NEW_LINE) {
            $db->logMessage($isNewLine, $id, "" ,$message, $name);
        } else {
            if ($insertTab)
                $tab = "=====";

            if ($message != null) {
                if ($skipHeader)
                    $name = "";
                $db->logMessage($isNewLine, $id, $tab . "" . $name . " ", $message, $name);
            }
        }
    }

    private static function addScreenMessageAndLog($room, $isNewLine, $playerMessage, $othersMessage = null, $logMessage = null, $name = null, $insertTab = true, $skipHeader) {
        ScreenDisplay::addScreenMessageOnly($room, $isNewLine, $playerMessage, $othersMessage, $name, $insertTab, $skipHeader);
        if (null == $logMessage) {
            $logMessage = $playerMessage;
        }
        ScreenDisplay::addLogMessage($room, $isNewLine, $logMessage, $name, $insertTab, $skipHeader);
    }

}

?>